package com.trkj.service;


import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.trkj.dao.DeptDAO;
import com.trkj.dao.EmpDAO;
import com.trkj.dao.LeaveDAO;
import com.trkj.dao.PositionDAO;
import com.trkj.dao.YearleaveDAO;
import com.trkj.pojo.Dept;
import com.trkj.pojo.Emp;
import com.trkj.pojo.Leave;
import com.trkj.pojo.Position;
import com.trkj.vo.LeaveVo;

/**
 * 请假申请
 * @author 彭帅
 *
 */
@Service
@Transactional
public class LeaveService {
	@Autowired
	EmpDAO empDAO;
	@Autowired
	DeptDAO deptDAO;
	@Autowired
	PositionDAO positionDAO;
	@Autowired
	YearleaveDAO yearleaveDAO;
	@Autowired
	LeaveDAO leaveDAO;
	public LeaveVo leaveVo(Integer empId) {
		try {
			SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
			Emp emp = empDAO.findOne(empId);
			LeaveVo leaveVo = new LeaveVo();
			leaveVo.setEmpId(emp.getEmpId());
			leaveVo.setEmpName(emp.getEmpName());
			leaveVo.setDeptId(emp.getDept().getDeptId());
			leaveVo.setDeptName(emp.getDept().getDeptName());
			leaveVo.setPositionId(emp.getPosition().getPositionId());
			leaveVo.setPositionName(emp.getPosition().getPositionName());
			leaveVo.setYearleaveSurplus(emp.getYearleave().getYearleaveSurplus());
			Date date = simpleDateFormat.parse(simpleDateFormat.format(new Date()));
			int a = date.getYear()+1900;
			int b = date.getMonth()+1;
			int c = date.getDate();
			leaveVo.setLeaveUptime(a+"-"+b+"-"+c);
			leaveVo.setLunchPeople(emp.getEmpId());
			return leaveVo;
		} catch (ParseException e) {
			e.printStackTrace();
		}
		return null;
	}
	/**
	 * 保存请假申请
	 * @param leave
	 * @param positionId
	 * @param empId
	 * @param deptId
	 * @return
	 */
	public Integer save_leave(Leave leave ,Integer positionId,Integer empId,Integer deptId) {
		try {
			if (leave != null) {
				if (positionId != null) {
					Position position = positionDAO.findOne(positionId);
					leave.setPosition(position);
				}
				if (empId != null) {
					Emp emp = empDAO.findOne(empId);
					leave.setEmp(emp);
				}
				if (deptId != null) {
					Dept dept = deptDAO.findOne(deptId);
					leave.setDept(dept);
				}
				return leaveDAO.save(leave).getLeaveId();
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return 0;
	}
}
